package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ads.tpgrupo4.dtos.ItemListaComparativaDePreciosDTO;
import com.ads.tpgrupo4.dtos.ListaComparativaDePreciosSearchDTO;
import com.ads.tpgrupo4.server.service.comparativaDePrecios.ComparativaDePreciosService;
import com.ads.tpgrupo4.server.utils.JsonUtils;

@WebServlet("/ComparativaDePreciosService")
public class ComparativaDePreciosServlet  extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private ComparativaDePreciosService comparativaDePreciosService = ComparativaDePreciosService.getInstance();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		final ListaComparativaDePreciosSearchDTO search = new ListaComparativaDePreciosSearchDTO();
		search.setNroDeSerie(req.getParameter("nroDeSerie"));
		search.setSufijo(req.getParameter("sufijo"));
		search.setMarca(req.getParameter("marca"));
		search.setOrigen(req.getParameter("origen"));
		
		req.setCharacterEncoding("utf8");
		resp.setContentType("application/json"); 
        PrintWriter out = resp.getWriter();
        
		final List<ItemListaComparativaDePreciosDTO> items = this.comparativaDePreciosService
				.searchInfoComparativaDePreciosDeRodamientos(search);
	
		final String result = JsonUtils.toJson(items);
        out.write(result);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		this.doGet(req, resp);
	}

}
